﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALOrderCompain
    /// </summary>
    public partial class DALOrderCompain
    {
        public DALOrderCompain()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbOrderCompain");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbOrderCompain model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbOrderCompain(");
            strSql.Append("OrderCode,CompainId,Content,Process,AddDate,ProcessDate)");
            strSql.Append(" values (");
            strSql.Append("@OrderCode,@CompainId,@Content,@Process,@AddDate,@ProcessDate)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.VarChar,50),
					new SqlParameter("@CompainId", SqlDbType.Int,4),
					new SqlParameter("@Content", SqlDbType.NVarChar,500),
					new SqlParameter("@Process", SqlDbType.NVarChar,500),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@ProcessDate", SqlDbType.DateTime)};
            parameters[0].Value = model.OrderCode;
            parameters[1].Value = model.CompainId;
            parameters[2].Value = model.Content;
            parameters[3].Value = model.Process;
            parameters[4].Value = model.AddDate;
            parameters[5].Value = model.ProcessDate;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbOrderCompain model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbOrderCompain set ");
            strSql.Append("OrderCode=@OrderCode,");
            strSql.Append("CompainId=@CompainId,");
            strSql.Append("Content=@Content,");
            strSql.Append("Process=@Process,");
            strSql.Append("AddDate=@AddDate,");
            strSql.Append("ProcessDate=@ProcessDate");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.VarChar,50),
					new SqlParameter("@CompainId", SqlDbType.Int,4),
					new SqlParameter("@Content", SqlDbType.NVarChar,500),
					new SqlParameter("@Process", SqlDbType.NVarChar,500),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@ProcessDate", SqlDbType.DateTime),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.OrderCode;
            parameters[1].Value = model.CompainId;
            parameters[2].Value = model.Content;
            parameters[3].Value = model.Process;
            parameters[4].Value = model.AddDate;
            parameters[5].Value = model.ProcessDate;
            parameters[6].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbOrderCompain ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbOrderCompain ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbOrderCompain GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,OrderCode,CompainId,Content,Process,AddDate,ProcessDate from tbOrderCompain ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            Gift.Model.tbOrderCompain model = new Gift.Model.tbOrderCompain();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderCode"] != null && ds.Tables[0].Rows[0]["OrderCode"].ToString() != "")
                {
                    model.OrderCode = ds.Tables[0].Rows[0]["OrderCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompainId"] != null && ds.Tables[0].Rows[0]["CompainId"].ToString() != "")
                {
                    model.CompainId = int.Parse(ds.Tables[0].Rows[0]["CompainId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Content"] != null && ds.Tables[0].Rows[0]["Content"].ToString() != "")
                {
                    model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Process"] != null && ds.Tables[0].Rows[0]["Process"].ToString() != "")
                {
                    model.Process = ds.Tables[0].Rows[0]["Process"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AddDate"] != null && ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                {
                    model.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProcessDate"] != null && ds.Tables[0].Rows[0]["ProcessDate"].ToString() != "")
                {
                    model.ProcessDate = DateTime.Parse(ds.Tables[0].Rows[0]["ProcessDate"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrderCode,CompainId,Content,Process,AddDate,ProcessDate ");
            strSql.Append(" FROM tbOrderCompain ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,OrderCode,CompainId,Content,Process,AddDate,ProcessDate ");
            strSql.Append(" FROM tbOrderCompain ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbOrderCompain ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from tbOrderCompain T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet GetOrderCompainList(string sOrderCode, int iCompainId, int iProcessd)
        {
            string sSQL = string.Empty;
            sSQL = "select Id,(select top 1 orderid from DB_Order where ordercode = tbOrderCompain.OrderCode) as orderid, OrderCode,(select CompainName from tbCompain where CompainId = tbOrderCompain.CompainId) as CompainName,CompainId,[Content],Process,AddDate,ProcessDate From tbOrderCompain"
                   + " where OrderCode like '%'+ @OrderCode +'%' ";
            if (iCompainId != -1)
            {
                sSQL = sSQL + "  and CompainId=@CompainId ";
            }
            if (iProcessd == 1)
            {
                sSQL = sSQL + " and isnull(Process,'') != '' ";
            }
            else
            {
                sSQL = sSQL + " and isnull(Process,'') = '' ";
            }
            sSQL = sSQL + " order by AddDate desc";
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,50),
                                        new SqlParameter("@CompainId", SqlDbType.Int,4),
                                        new SqlParameter("@Processd", SqlDbType.Int,4)};
            parameters[0].Value = sOrderCode;
            parameters[1].Value = iCompainId;
            parameters[2].Value = iProcessd;

            return DbHelperSQL.Query(sSQL, parameters);

        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbOrderCompain GetModelByOrderCode(string sOrderCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrderCode,CompainId,Content,Process,AddDate,ProcessDate from tbOrderCompain ");
            strSql.Append(" where OrderCode=@OrderCode ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.VarChar,50)};
            parameters[0].Value = sOrderCode;

            Gift.Model.tbOrderCompain model = new Gift.Model.tbOrderCompain();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.OrderCode = ds.Tables[0].Rows[0]["OrderCode"].ToString();
                if (ds.Tables[0].Rows[0]["CompainId"].ToString() != "")
                {
                    model.CompainId = int.Parse(ds.Tables[0].Rows[0]["CompainId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                {
                    model.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProcessDate"].ToString() != "")
                {
                    model.ProcessDate = DateTime.Parse(ds.Tables[0].Rows[0]["ProcessDate"].ToString());
                }
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                model.Process = ds.Tables[0].Rows[0]["Process"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbOrderCompain";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

